<template>
  <div class="layout-container" :class="{ old: elder }">
    <div class="layout-section layout-section2">
      <top-header :show-elder="false" :auth-check="true" @setElder="setElderByChild"></top-header>

      <div class="table_main qydj-container" v-html="info.archiveHtml"></div>

      <div class="item_btn" style="margin-bottom: 20px;">
        <!--van-button plain type="primary" @click="reGain(index)">重新获取</van-button-->
        <van-button plain type="info" size="small" @click="showArchive('view')">查看档案</van-button>&nbsp;&nbsp;
        <van-button plain type="info" size="small" @click="showArchive('download')">下载到本地</van-button>&nbsp;&nbsp;
        <van-button plain type="warning" size="small" @click="goList">返回列表</van-button>
      </div>

      <footer-right></footer-right>
    </div>

    <!--pdf预览-->
    <van-dialog v-model="pdfContainer.show" title="查看企业档案" :show-confirm-button="false" :show-cancel-button="false">
      <pdf :src="pdfContainer.url" :page="pdfContainer.curPage" @num-pages="pdfContainer.totalPage - $event"></pdf>
      <div style="margin: 10px;">
        <div style="float:left; line-height: 35px; height: 35px; ">{{ pdfContainer.curPage }}/{{ pdfContainer.totalPage }}</div>
        <div style="float: right;">
          <van-button type="warning" size="small" @click="prePage">上一页</van-button>
          <van-button type="warning" size="small" @click="nextPage">下一页</van-button>
          <van-button type="danger" size="small" @click="pdfContainer.show=false">关闭窗口</van-button>
        </div>
      </div>
    </van-dialog>

    <footer-nav :active-index="-1"></footer-nav>
  </div>
</template>

<script>
import pdf from "vue-pdf";
import FooterNav from "@/components/footerNav";
import FooterRight from "@/components/footerRight";
import TopHeader from "@/components/topHeader";

import {apiDetailBase, apiDetailChange, apiDownloadPdf} from '@/api/applyDetail'
import {eraqConstants} from "@/utils/lwUtils";

export default {
  components: {FooterRight, FooterNav, TopHeader, pdf},
  data() {
    return {
      elder: false,
      info: {
        unid: "", infoType: "", archiveHtml: "",
      },
      pdfContainer: {
        show: false, url: "", pages: "", curPage: 1, totalPage: 0
      },
    }
  },
  created() {
    this.info.unid = this.$route.query.unid;
    this.info.infoType = this.$route.query.type;
    this.loadDetail(this.info.infoType, this.info.unid);
  },
  methods: {
    //子组件调用方法，设置老年模式
    setElderByChild: function (elder) {
      this.elder = elder;
    },
    loadDetail: function (infoType, unid) {
      console.log("infoType:"+infoType)
      console.log(infoType === "0")
      if (infoType === "0") {
        apiDetailBase(unid).then(res => {
          this.showDetail(res);
        }).catch(msg => {
          this.$eraqDlg.error(typeof msg === "undefined" || msg === "" ? "获取企业基本异常" : msg);
        });
      } else {
        apiDetailChange(unid).then(res => {
          this.showDetail(res);
        }).catch(msg => {
          this.$eraqDlg.error(typeof msg === "undefined" || msg === "" ? "获取企业变更异常" : msg);
        });
      }
    },
    showDetail: function (res) {
      this.errorMsg = res.msg;
      if ("" !== res.msg) {
        this.info.archiveHtml = "";
      } else {
        this.info.unid = res.data.fileBelongto;
        this.info.infoType = res.data.fileType;
        this.info.archiveHtml = res.data.fileMd5;
      }
    },
    goList: function () {
      this.$router.push({path: '/list'});
    },
    //查看档案-主入口
    showArchive: function (flag) {
      if (this.info.unid === undefined || this.info.infoType === undefined || this.info.unid === "" || this.info.infoType === "") {
        this.$eraqDlg.error("请求参数有误");
        return;
      }

      apiDownloadPdf(this.info.unid, this.info.infoType, "").then(res => {
        if (res.msg !== "") {
          this.errorMsg = res.msg;
          return;
        }

        //获得档案文件地址
        //let archiveUrl = "https://qyda.tzscjg.cn:8090/eraq-api/profile/help/manual.pdf";
        if (flag !== "download") {
          this.showPdf(res.data.filePathObs);
          return;
        }
        this.downloadPdf(res.data.filePathObs);
      }).catch((msg) => {
        this.$eraqDlg.error(msg === "" ? "档案生成异常" : msg);
      });
    },
    //查看档案-在线预览
    showPdf: function (fileUrl) {
      this.pdfContainer.url = pdf.createLoadingTask(fileUrl);
      this.pdfContainer.url.promise.then(pdf => {
        this.$nextTick(() => {
          this.pdfContainer.totalPage = pdf.numPages;
          this.pdfContainer.curPage = 1;
        });
        this.pdfContainer.show = true;
      });
    },
    //查看档案-下载到本地
    downloadPdf: function (fileUrl) {
      ZWJSBridge.saveImage({
        url: fileUrl
      }).then((result) => {
        console.log(result);
      }).catch((error) => {
        console.log(error);
      });
    },

    //在线预览翻页
    prePage: function () {
      let page = this.pdfContainer.curPage;
      page = page > 1 ? page - 1 : this.pdfContainer.totalPage;
      this.pdfContainer.curPage = page;
    },
    nextPage: function () {
      let page = this.pdfContainer.curPage;
      page = page < this.pdfContainer.totalPage ? page + 1 : 1;
      this.pdfContainer.curPage = page;
    },
  },
}
</script>

<style lang='scss' scoped>
@import "src/css/home";
@import "src/css/jzy";
@import "src/css/detail";

</style>
